let titleText = [
	{name:'网页title',value:'101'},
	{name:'考试页文字标题',value:'102'},
]
let logoImg = [
	{name:'登录页logo图片',value:'201'},
	{name:'后台主页logo',value:'202'},
	{name:'考试登录页logo',value:'203'},
	{name:'考试页logo',value:'204'},
	{name:'网页iconLogo图标',value:'205'},
]
let bgImg = [
	{name:'登录页背景图片',value:'301'},
	{name:'考试登录页背景图片',value:'302'},
	{name:'考试过渡页背景图片',value:'303'},
]

let colorText = [
	{name:'答题卡颜色',value:'401'},
	{name:'登录按钮颜色',value:'402'},
	{name:'考试页文字颜色',value:'403'},
	{name:'主题类型',value:'404'},
]
let themeColorArr = [
	{name:'主题类型',value:'501'},
]
$().ready(function() {
	inputChange()
	validateRule();
	uploadCreate()
	imageCreate()
	colorChange()
	valueChange()
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

//输入框事件
function inputChange() {
	$('#type').on('change', function() {
		$('#value').val('')
		var inputValue = $.trim($(this).val().toString());
		updateSecondOptions(inputValue)
		if(inputValue === "1") {
			$('#uploadBox').hide()
			$('#colorBox').hide()
			$('#theme').hide()
		}else if (inputValue === "2" || inputValue === "3") {
			$('#uploadBox').show()
			$('#colorBox').hide()
			$('#theme').hide()
		}else if(inputValue === "4") {
			$('#colorBox').show()
			$('#uploadBox').hide()
			$('#theme').hide()
		}else if(inputValue === "5"){
			$('#theme').show()
			$('#uploadBox').hide()
			$('#colorBox').hide()
		}
	});

}

// 值输入框事件
function valueChange() {
	$('#value').on('input', function() {
		$('#colorComponent').val($(this).val())
		$('#themeInput').val($(this).val())
	});
}


// 二级选择器
function updateSecondOptions(value) {
	// 清空二级选择框的选项
	$('#second-select').empty();

	switch (value) {
		case '1':
			listdata(titleText)
			return;
		case '2':
			listdata(logoImg)
			return;
		case '3':
			listdata(bgImg)
			return;
		case '4':
			listdata(colorText)
			return;
		case '5':
			listdata(themeColorArr)
			return;
		default:
			break;
	}
}

function  listdata(value) {
	value.forEach((element,index) => {
		$('#second-select').append(`<option value="${element.value}">${element.name}</option>`);
	});
}

// 颜色选择器事件
function colorChange() {
	$('#colorComponent').on('change',function (e) {
		$('#value').val(e.target.value)
	})
}

// 图片展示
function imageCreate() {
	$('#fileImg').on('click',function () {
		var imageUrl = $(this).attr('src');
		$('.overlay').toggleClass('active');
		$('.overlay img').attr('src', imageUrl);
	})
	$('.overlay').click(function() {
		$(this).removeClass('active');
	});
}
var url = "/common/sysFile/upload";
// 上传函数
function uploadCreate() {
	// 监听文件选择按钮的 change 事件
	$('#upload-button').on('change', function() {
		var file = this.files[0];

		// 创建 FormData 对象
		var formData = new FormData();
		formData.append('file', file);

		// 发送 POST 请求到后端
		$.ajax({
			url: url,
			type: 'POST',
			data: formData,
			processData: false,
			contentType: false,
			success: function(response) {
				if(response.code === '0') {
					$('#value').val(response.data)
					$('#fileImg').attr('src',`${response.data}`)
					$('#fileImg').show()
					return
				}
				layer.msg(response.msg);
			},
			error: function() {
				layer.msg('上传失败');
			}
		});
	});
}

// 新增
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/kawmType/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {

	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			type : {
				required : true
			},
			value : {
				required : true
			}
		},
		messages : {
			type : {
				required : icon + "请输入类型"
			},
			value : {
				required : icon + "请输入值"
			}
		}
	})
}
